<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../include/taglibs.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>夺宝商品列表</title>
    <style type="text/css">
        html, body {
            margin: 0;
            padding: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

    </style>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <link href="/css/demo.css" rel="stylesheet" type="text/css"/>
    <script src="/scripts/boot.js" type="text/javascript"></script>
</head>
<body>

<div style="padding-bottom:5px;padding-left: 0px;">

    <div id="form1">

        <table border="0" cellpadding="1" cellspacing="2">
            <tr>
            <td>商品状态：</td>
					<td>
					<input name="status" class="mini-combobox" style="width:150px;" onvaluechanged="search()"
                                   textField="text" valueField="code" emptyText="请选择..."
                                   url="/product/getProductStatus.action" showNullItem="true"
                                   nullItemText="请选择..."/>
                                   </td>
                <td>
                   	 商品名称：
                </td>
                <td>
                    <input name="productNameOrId" id="productName" emptyText="请输入商品名称或ID" class="mini-textbox"/>
                </td>
                <td colspan="2" align="center"><a class="mini-button" iconCls="icon-search" onClick="search()">搜索</a>&nbsp;<a
                        class="mini-button" iconCls="icon-reload" onClick="clean()">重置</a>
                </td>
            </tr>
        </table>
    </div>
</div>
<div style="width:99%;">
    <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <a class="mini-button" iconCls="icon-add" onclick="add()">增加</a>
                    <a class="mini-button" iconCls="icon-edit" onclick="batchEditProductStatus(0)">批量上架</a>
                    <a class="mini-button" iconCls="icon-edit" onclick="batchEditProductStatus(1)">批量下架</a>
                </td>
            </tr>
        </table>
    </div>
</div>
<div id="datagrid1" class="mini-datagrid" style="width:99%;height:90%;" multiSelect="true"
     url="/product/queryProduct.action" idField="id" allowResize="true" allowCellWrap="true"
     >
    <div property="columns">
        <div type="checkcolumn"></div>
        <div field="id" width="80" headerAlign="center" align="center">商品ID</div>
        <div field="nameEn" width="140" headerAlign="center" align="center">商品名称</div>
        <div field="image" width="80" headerAlign="center" align="center">商品图片</div>
        <div field="subtitleEn" width="140" headerAlign="center" align="center">商品描述</div>
        <div field="productTypeEnum.text" width="80" headerAlign="center" align="center">商品类型</div>
        <div field="productPostTypeEnum.text" width="80" headerAlign="center" align="center">寄送类型</div>
        <div field="productStatusEnum.text" width="80" headerAlign="center" align="center">商品状态</div>
        <div field="countryCodeEnum.text" width="80" headerAlign="center" align="center">活动地区</div>
		<div name="action" width="100" headerAlign="center" align="center"
				renderer="onActionRenderer" cellStyle="padding:0;">操作</div>
    </div>
</div>

<script type="text/javascript">

    mini.parse();
    var grid = mini.get("datagrid1");
    var form = new mini.Form("#form1"); //获取表单对象
    grid.load();
    //查询
    function search() {
        var data = form.getData();      //获取表单多个控件的数据
        var json = mini.encode(data);   //序列化成JSON
        grid.load({json: json});
    }

    function clean() {
        form.reset();
    }

	function onActionRenderer(e) {
		var record = e.record;
		var uid = record.id;
		var rowIndex = e.rowIndex;
		var s = ' <a class="Edit_Button" href="javascript:edit(\'' + uid
				+ '\')" >Edit</a>'
				+ ' <a class="Edit_Button" href="javascript:detail(\''
				+ uid + '\')">Detail</a>';
		return s;
	}

	function detail(){
    	var row = grid.getSelected();
		mini.open({
            url: "/gamblingPeriod/findPeriod.action",
            title: "期数列表",
            width: 1234,
            height: 555,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = {productId: row.id};
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
	}
    function add() {
        mini.open({
            url: "<c:url value='/product/addProductPage.action'/>",
            title: "夺宝商品添加", width: 999, height: 666,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = {action: "new"};
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                if (action == "save") {
                    grid.reload();
                    mini.showTips({
                        content: "<b>成功</b> <br/>商品添加成功",
                        state: "success",
                        x: "center",
                        y: "center",
                        timeout: 3000
                    });
                }
            }
        });
    }
    function edit(id) {
        var row = grid.getSelected();
        if (row) {
            mini.open({
                url: "<c:url value='/product/addProductPage.action'/>",
                title: "夺宝商品修改", width: 999, height: 666,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = {action: "edit", id: row.id};
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    if (action == "save") {
                        grid.reload();
                        mini.showTips({
                            content: "<b>成功</b> <br/>商品修改成功",
                            state: "success",
                            x: "center",
                            y: "center",
                            timeout: 3000
                        });
                    }
                }
            });
        }
    }
    function batchEditProductStatus(e) {
        var rows = grid.getSelecteds();
        var warnText;
        var successText;
        if(e==0){
        	warnText = "确定上架选中记录？";
        	successText = "上架";
        }else if(e==1){
        	warnText = "确定下架选中记录？";
        	successText = "下架";
        }
        if (rows.length > 0) {
            var firstRow = rows[0].productStatusEnum.code;
            for(var i = 0; i < rows.length; i++)
			{
				if(i>0){
					var flag = (firstRow==rows[i].productStatusEnum.code);
					if(!flag){
						alert("请保证状态一致!");
						return;
					}
				}
			}
            if (confirm(warnText)) {
                var ids = [];
                for (var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var productId = ids.join(',');
                grid.loading("操作中，请稍后......");
                $.ajax({
                    url: "/product/batchEditProductStatus.action?ids="+productId+"&flag="+e,
                    success: function () {
                        grid.reload();
                        mini.showTips({
                            content: "<b>成功</b> <br/>"+successText+"成功",
                            state: "success",
                            x: "center",
                            y: "center",
                            timeout: 3000
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        grid.reload();
                        mini.showMessageBox({
                            showModal: false,
                            width: 250,
                            title: "提示",
                            iconCls: "mini-messagebox-warning",
                            message: jqXHR.responseText,
                            timeout: 3000,
                            x: "right",
                            y: "bottom"
                        });
                    }
                });
            }
        } else {
            alert("请选中一条记录");
        }
    }
</script>
</body>
</html>
